package com.cennavi.bigdata.baotou.service.impl;

import com.cennavi.bigdata.baotou.dao.EmergencyDao;
import com.cennavi.bigdata.baotou.service.EmergencyService;
import com.cennavi.bigdata.baotou.util.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by cennavi on 2018/10/17.
 */
@Service
public class EmergencyServiceImpl implements EmergencyService {

    @Autowired
    private EmergencyDao emergencyDao;

    @Override
    public List<Map<String, Object>> listEmergencyRoll(int count) {

        String dateStr = DateUtils.DateFormatUnit.DATE.getDateStr(new Date())+" 23:59:59";
        List<Map<String,Object>> list = emergencyDao.listEmergencyRoll(dateStr,count);
        for (Map<String, Object> map : list) {
            //处置过程（1已接警、2处置中、3反馈中、9结束）
            map.put("HANDLETYPE",StringUtils.isBlank((String) map.get("HANDLETYPE"))?"1":map.get("HANDLETYPE").toString());
            //影响程度（1轻微、2中等、3严重）
            map.put("INFLUENCETYPE",StringUtils.isBlank((String) map.get("INFLUENCETYPE"))?"1":map.get("INFLUENCETYPE").toString());
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> listEmergency(String startTime, String endTime, String type, String handletype, String influencetype, int count) {

        List<Map<String, Object>> list = emergencyDao.listEmergency(startTime+" 00:00:00",endTime+" 23:59:59",type,handletype,influencetype,count);
        for (Map<String, Object> map : list) {
            //处置过程（1已接警、2处置中、3反馈中、9结束）
            map.put("HANDLETYPE",StringUtils.isBlank((String) map.get("HANDLETYPE"))?"1":map.get("HANDLETYPE").toString());
            //影响程度（1轻微、2中等、3严重）
            map.put("INFLUENCETYPE",StringUtils.isBlank((String) map.get("INFLUENCETYPE"))?"1":map.get("INFLUENCETYPE").toString());
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> listEmergencyCount(String startTime, String endTime, String statisticsType, String type, String handletype, String influencetype) {

        List<Map<String, Object>> list = null;
        if (statisticsType.equals("1")){
            list =  emergencyDao.listEmergencyCountByType(startTime+" 00:00:00",endTime+" 23:59:59",type,handletype,influencetype);
        }else if (statisticsType.equals("2")){
            list =  emergencyDao.listEmergencyCountByHandletype(startTime+" 00:00:00",endTime+" 23:59:59",type,handletype,influencetype);
            for (Map<String, Object> map : list) {
                //处置过程（1已接警、2处置中、3反馈中、9结束）
                String TYPE = map.get("TYPE").toString();
                if (TYPE.equals("1")){
                    map.put("TYPENAME","已接警");
                }else if (TYPE.equals("2")){
                    map.put("TYPENAME","处理中");
                }else if (TYPE.equals("3")){
                    map.put("TYPENAME","反馈中");
                }else if (TYPE.equals("9")){
                    map.put("TYPENAME","已结束");
                }
            }
        }else if (statisticsType.equals("3")){
            list =  emergencyDao.listEmergencyCountByInfluencetype(startTime+" 00:00:00",endTime+" 23:59:59",type,handletype,influencetype);
            for (Map<String, Object> map : list) {
                //影响程度（1轻微、2中等、3严重）
                String TYPE = map.get("TYPE").toString();
                if (TYPE.equals("1")){
                    map.put("TYPENAME","轻微");
                }else if (TYPE.equals("2")){
                    map.put("TYPENAME","中等");
                }else if (TYPE.equals("3")){
                    map.put("TYPENAME","严重");
                }
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> listType() {
        List<Map<String, Object>> list = emergencyDao.listType();
        Map<String, Object> map = new HashMap<>();
        map.put("TYPE","0");
        map.put("TYPENAME","全部");
        list.add(map);
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return o1.get("TYPE").toString().compareTo(o2.get("TYPE").toString());
            }
        });
        return list;
    }
}
